package com.test04.baiduapi;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;

import org.junit.Test;
/*
 * 这是用来调用百度的api来解析手机号的归属地
 * 总结：
 *    1.这是按照百度给的文档写的，原理上就是：
 *    	1.get请求http://apis.baidu.com/apistore/mobilenumber/mobilenumber?phone=xxxxxx
 *    	2.设置header("apikey", "733bf50c328946282f731bfeb2775bd9")
 *    	3.得到相应unicode数据，再把unicode解析成中文
 */
public class PhoneParse01 {
	public static void main(String[] args) throws Exception {
		String httpUrl = "http://apis.baidu.com/apistore/mobilenumber/mobilenumber";
		String httpArg = "phone=18810629868";
		String jsonResult = request(httpUrl, httpArg);
		
		System.out.println(jsonResult);
		System.out.println(unicode2String(jsonResult));
	}

	public static String request(String httpUrl, String httpArg) throws Exception {
		BufferedReader reader = null;
		String result = null;
		StringBuffer sb = new StringBuffer();
		httpUrl = httpUrl + "?" + httpArg;

		URL url = new URL(httpUrl);
		HttpURLConnection conn = (HttpURLConnection) url.openConnection();

		conn.setRequestMethod("GET");
		conn.setRequestProperty("apikey", "733bf50c328946282f731bfeb2775bd9");
		conn.connect();

		InputStream is = conn.getInputStream();
		reader = new BufferedReader(new InputStreamReader(is, "utf-8"));
		String line = null;
		while ((line = reader.readLine()) != null) {
			sb.append(line);
			sb.append("\r\n");
		}
		reader.close();
		result = sb.toString();

		return result;
	}
	/**
	 * 字符串转换unicode
	 */
	public static String string2Unicode(String string) {
	    StringBuffer unicode = new StringBuffer();
	    for (int i = 0; i < string.length(); i++) {
	        // 取出每一个字符
	        char c = string.charAt(i);
	        // 转换为unicode
	        unicode.append("\\u" + Integer.toHexString(c));
	    }
	    return unicode.toString();
	}
	
	@Test
	public void test(){
		String string2Unicode = string2Unicode("北京");
		String unicode2String = unicode2String(string2Unicode);
		
		System.out.println(string2Unicode);
		System.out.println(unicode2String);
	}
	
	/**
	 * unicode 转换成 中文
	 */
	public static String unicode2String(String theString) {
		char aChar;
		int len = theString.length();
		StringBuffer outBuffer = new StringBuffer(len);
		for (int x = 0; x < len;) {
			aChar = theString.charAt(x++);
			if (aChar == '\\') {
				aChar = theString.charAt(x++);
				if (aChar == 'u') {
					int value = 0;
					for (int i = 0; i < 4; i++) {
						aChar = theString.charAt(x++);
						switch (aChar) {
						case '0':
						case '1':
						case '2':
						case '3':
						case '4':
						case '5':
						case '6':
						case '7':
						case '8':
						case '9':
							value = (value << 4) + aChar - '0';
							break;
						case 'a':
						case 'b':
						case 'c':
						case 'd':
						case 'e':
						case 'f':
							value = (value << 4) + 10 + aChar - 'a';
							break;
						case 'A':
						case 'B':
						case 'C':
						case 'D':
						case 'E':
						case 'F':
							value = (value << 4) + 10 + aChar - 'A';
							break;
						default:
							throw new IllegalArgumentException("Malformed   \\uxxxx   encoding.");
						}
					}
					outBuffer.append((char) value);
				} else {
					if (aChar == 't')
						aChar = '\t';
					else if (aChar == 'r')
						aChar = '\r';
					else if (aChar == 'n')
						aChar = '\n';
					else if (aChar == 'f')
						aChar = '\f';
					outBuffer.append(aChar);
				}
			} else
				outBuffer.append(aChar);
		}
		return outBuffer.toString();

	}
	public static void append(File file,String line) throws IOException{
		BufferedWriter bw = new BufferedWriter(new FileWriter(file, true));
		bw.write(line);
		bw.close();
	}
}
